package admin.controllers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.UserModel;

import admin.data.UserData;

public class UpdateUserServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException {
    String strIds = (String) request.getParameter("ids");
    String action = (String) request.getParameter("action");
    int status = 0;
    String[] ids = strIds.split(",");
    UserData uData = new UserData();
    
    ArrayList<String> statusAction = new ArrayList<String>(Arrays.asList("active", "pending", "inactive"));
    
    for (int i = 0; i < ids.length; i++) {
        if (!ids[i].isEmpty()) {
        	if(statusAction.contains(action.toLowerCase())) {
                if (action.equalsIgnoreCase("active")) {
                	status = 2;
                } else if (action.equalsIgnoreCase("pending")) {
					status = 1;
				} else if (action.equalsIgnoreCase("inactive")) {
					status = 0;
				}
                
                UserModel user = uData.getUser(ids[i]);
                if (user != null) {
        			user.setStatus(status);
        		}
                uData.insert(user);
        	} else if (action.equalsIgnoreCase("remove")) {
        		UserModel user = uData.getUser(ids[i]);
				uData.delete(user);
			}
        }
    }
}
}
